package Protocol;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.*;

/**
 * Het communicatie protocol welke de regels bevat voor communicatie tussen de server en de client
 * Plus de benodigde transport methodes
 * @author Michael Stevens
 * @since 03-05-2012
 */
public class ComProtocol {
    private Socket socket;
    private DataInputStream input;
    private ObjectOutputStream oOutput;
    private ObjectInputStream oInput;
        
    //acties
    private final String selectQuery = "stq";
    private final String authSelectQuery = "astq";
    private final String updateQuery = "udq";
    private final String inloggen = "inloggen";
    private final String inloggenMaster = "inloggenMaster";
    private final String uitloggen = "uitloggen";
    private final String aanmaken = "aanGeb";
    private final String createBackup = "crback";
    private final String recoverBackup = "recback";
    private final String updateBackupList = "upbacklst";
    private final String deleteBackup = "delbck";
    
    public ComProtocol(Socket socket){
        this.socket = socket;
    }
    
    public ComProtocol(String server, int port){
        try{
            socket = new Socket(server, port);
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
    }
    
    /**
     * Ontvangt een verzonden object via een ObjectInputStream
     * @return Ontvangen object
     * @author Michael Stevens
     * @since 03-05-2011
     */
    public Object recieve(){
        Object o = null;
        try{
            this.oInput = new ObjectInputStream(socket.getInputStream());
            o = oInput.readObject();
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
        return o;
        
    }
    
    /**
     * Verstuurt een gegeven object via een ObjectOutputStream
     * @author Michael Stevens
     * @since 03-05-2011
     */
    public void send(Object o){
        try{
            oOutput = new ObjectOutputStream(socket.getOutputStream());
            oOutput.writeObject(o);
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
    }
    
    /**
     * Stopt alle in en output streams plus de socket mits deze aangemaakt zijn     
     * @author Michael Stevens
     * @since 03-05-2011
     */
    public void close(){
        try{
            if(input != null){
                input.close();
            }
            if(oOutput != null){
                oOutput.close();
            }
            if(oInput != null){
                oInput.close();
            }
            if(socket != null){                
                socket.close();
            }
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
    }

    /**
     * @return the selectQuery
     */
    public String getSelectQuery() {
        return selectQuery;
    }

    /**
     * @return the updateQuery
     */
    public String getUpdateQuery() {
        return updateQuery;
    }

    /**
     * @return the inloggen
     */
    public String getInloggen() {
        return inloggen;
    }

    /**
     * @return the uitloggen
     */
    public String getUitloggen() {
        return uitloggen;
    }

    /**
     * @return the inloggenMaster
     */
    public String getInloggenMaster() {
        return inloggenMaster;
    }

    /**
     * @return the authSelectQuery
     */
    public String getAuthSelectQuery() {
        return authSelectQuery;
    }
    
    /**
     * @return the backupCreateQuery
     */
    public String getBackupCreateQuery() {
        return createBackup;
    }
    
    /**
     * @return the backupCreateQuery
     */
    public String getBackupLoadQuery() {
        return recoverBackup;
    }
    
    /**
     * @return the backupCreateQuery
     */
    public String getUpdateBackupList() {
        return updateBackupList;
    }
    
    /**
     * @return the deleteBackup
     */
    public String getDeleteBackup() {
        return deleteBackup;
    }
}
